package com.train.course.service.impl;

import com.train.common.utils.SecurityUtils;
import com.train.course.domain.VideoCourse;
import com.train.course.domain.VideoTag;
import com.train.course.dto.CourseTagDTO;
import com.train.course.dto.VideoCourseDTO;
import com.train.course.mapper.VideoCourseMapper;
import com.train.course.service.IVideoCourseService;
import com.train.course.service.IVideoTagService;
import com.train.course.vo.VideoTagVO;
import com.train.course.vo.VideoCourseVO;
import com.train.framework.web.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 课程管理 服务层实现
 */
@Service
public class VideoCourseServiceImpl implements IVideoCourseService {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private VideoCourseMapper videoCourseMapper;

    @Autowired
    private IVideoTagService videoTagService;

    /**
     * 查询课程信息
     *
     * @param courseId 课程ID
     * @return 课程信息
     */
    @Override
    public VideoCourse selectVideoCourseById(Long courseId) {
        return videoCourseMapper.selectVideoCourseById(courseId);
    }




    /**
     * 查询课程列表
     *
     * @param videoCourse 课程信息
     * @return 课程集合
     */
    @Override
    public List<VideoCourse> selectVideoCourseList(VideoCourse videoCourse) {
        return videoCourseMapper.selectVideoCourseList(videoCourse);
    }

    /**
     * 新增课程
     *
     * @param videoCourse 课程信息
     * @return 结果
     */
    @Override
    public int insertVideoCourse(VideoCourse videoCourse) {
        return videoCourseMapper.insertVideoCourse(videoCourse);
    }

    /**
     * 新增课程（包含分类和标签）
     *
     * @param videoCourseDTO 课程信息DTO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVideoCourseWithTags(VideoCourseDTO videoCourseDTO) {
        // 1. 新增课程基本信息
        VideoCourse videoCourse = new VideoCourse();
        BeanUtils.copyProperties(videoCourseDTO, videoCourse);
        videoCourse.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        int result = videoCourseMapper.insertVideoCourse(videoCourse);
        
        // 2. 添加标签（如果有）
        if (result > 0 && videoCourseDTO.getTagNames() != null && !videoCourseDTO.getTagNames().isEmpty()) {
            CourseTagDTO courseTagDTO = new CourseTagDTO();
            courseTagDTO.setCourseId(videoCourse.getCourseId());
            courseTagDTO.setTagNames(videoCourseDTO.getTagNames());
            videoTagService.addCourseTags(courseTagDTO);
        }
        
        return result;
    }

    /**
     * 修改课程
     *
     * @param videoCourse 课程信息
     * @return 结果
     */
    @Override
    public int updateVideoCourse(VideoCourse videoCourse) {
        return videoCourseMapper.updateVideoCourse(videoCourse);
    }

    /**
     * 修改课程（包含分类和标签）
     *
     * @param videoCourseDTO 课程信息DTO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVideoCourseWithTags(VideoCourseDTO videoCourseDTO) {
        // 1. 修改课程基本信息
        VideoCourse videoCourse = new VideoCourse();
        BeanUtils.copyProperties(videoCourseDTO, videoCourse);
        int result = videoCourseMapper.updateVideoCourse(videoCourse);
        
        // 2. 更新标签（如果有）
        if (result > 0 && videoCourseDTO.getTagNames() != null) {
            CourseTagDTO courseTagDTO = new CourseTagDTO();
            courseTagDTO.setCourseId(videoCourse.getCourseId());
            courseTagDTO.setTagNames(videoCourseDTO.getTagNames());
            videoTagService.addCourseTags(courseTagDTO);
        }
        
        return result;
    }

    /**
     * 删除课程信息
     *
     * @param courseId 课程ID
     * @return 结果
     */
    @Override
    public int deleteVideoCourseById(Long courseId) {
        return videoCourseMapper.deleteVideoCourseById(courseId);
    }

    /**
     * 批量删除课程信息
     *
     * @param courseIds 需要删除的课程ID数组
     * @return 结果
     */
    @Override
    public int deleteVideoCourseByIds(Long[] courseIds) {
        return videoCourseMapper.deleteVideoCourseByIds(courseIds);
    }

    /**
     * 查询课程详情（包含标签）
     *
     * @param courseId 课程ID
     * @return 课程详情VO
     */
    @Override
    public VideoCourseVO selectVideoCourseDetailById(Long courseId) {
        VideoCourse videoCourse = videoCourseMapper.selectVideoCourseById(courseId);
        if (videoCourse == null) {
            return null;
        }
        
        VideoCourseVO videoCourseVO = new VideoCourseVO();
        BeanUtils.copyProperties(videoCourse, videoCourseVO);
        
        // 填充标签信息
        List<VideoTag> tags = videoTagService.selectTagsByVideoId(courseId, "1");
        if (tags != null && !tags.isEmpty()) {
            List<VideoTagVO> tagVOList = tags.stream().map(tag -> {
                VideoTagVO tagVO = new VideoTagVO();
                tagVO.setTagId(tag.getTagId());
                tagVO.setTagName(tag.getTagName());
                tagVO.setTagDesc(tag.getTagDesc());
                return tagVO;
            }).collect(Collectors.toList());
            videoCourseVO.setTags(tagVOList);
        }
        
        return videoCourseVO;
    }

}